package com.boco.webmaster.model;

import java.util.Map;
import java.util.HashMap;
import com.boco.transnms.server.bo.helper.cm.*;
import com.boco.transnms.client.model.base.BoCmdFactory;
import com.boco.transnms.client.model.base.IBoCommand;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.base.DataObjectList;
import com.boco.transnms.common.dto.Room;
import java.util.*;

public class RoomModel {
    private static RoomModel instance = new RoomModel();
    private Map roomMap = new HashMap();
    public RoomModel() {
        refresh();
    }

    public void refresh(){
        IBoCommand cmd = BoCmdFactory.getInstance().createBoCmd(RoomBOHelper.ActionName.getAllRoom, new Object[] {new BoActionContext()});
        DataObjectList dbos = (DataObjectList) cmd.exec();
        for (int i = 0; i < dbos.size(); i++) {
            Room dbo = (Room)dbos.get(i);
            roomMap.put(dbo.getCuid(),dbo);
        }
    }

    public static RoomModel getInstance() {
        return instance;
    }

    public Room getRoomByCuid(String cuid){
        return (Room)roomMap.get(cuid);
    }

    public String getCuidByLabelCn(String label_cn){
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String room_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(room_cuid);
            if(dbo.getLabelCn().equals(label_cn)){
                return dbo.getCuid();
            }
        }
        return null;
    }

    public List getAllRoomLabelCn(){
        List list = new ArrayList();
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String room_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(room_cuid);
            list.add(dbo.getLabelCn());
        }
        return list;
    }

    public boolean haveRoom(String label_cn){
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String room_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(room_cuid);
            if(dbo.getLabelCn().equals(label_cn)){
                return true;
            }
        }
        return false;
    }

    public void addRoom(Room room){
        roomMap.put(room.getCuid(),room);
    }

    public void delRoom(Room room){
        roomMap.remove(room.getCuid());
    }

    public void delRoom(Long objectId){
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String room_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(room_cuid);
            if(dbo.getObjectNum()==objectId){
                roomMap.remove(room_cuid); //modify by shandongxu 2007/01/18 dbo->room_cuid
                break;
            }
        }
    }

    public void modifyRoom(Room room){
        roomMap.remove(room.getCuid());
        roomMap.put(room.getCuid(),room);
    }

    public DataObjectList getAllRoom(){
        DataObjectList dbos = new DataObjectList();
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String site_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(site_cuid);
            dbos.add(dbo);
        }
        return dbos;
    }

    public DataObjectList getRoomBySiteCuid(String siteCuid){
        DataObjectList dbos = new DataObjectList();
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String site_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(site_cuid);
            if(dbo.getRelatedSpaceCuid().equals(siteCuid)){
                dbos.add(dbo);
            }
        }
        return dbos;
    }

    public String getRoomName(String cuid){
        String labelCn = "";
        Room dbo = (Room)roomMap.get(cuid);
        if(dbo!=null){
            labelCn = dbo.getLabelCn();
        }
        return labelCn;
    }

    public Room getRoomByLabelCn(String label_cn){
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String room_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(room_cuid);
            if(dbo.getLabelCn().equals(label_cn)){
                return dbo;
            }
        }
        return null;
    }

    public Room getRoomByRoomNameAndSiteCuid(String label_cn,String siteCuid){
        Iterator i = roomMap.keySet().iterator();
        while (i.hasNext()) {
            String room_cuid = (String) i.next();
            Room dbo = (Room) roomMap.get(room_cuid);
            if(dbo.getLabelCn().equals(label_cn) && dbo.getRelatedSpaceCuid().equals(siteCuid)){
                return dbo;
            }
        }
        return null;
    }
}
